package com.soft.base.exception;

import com.xiaoleilu.hutool.util.CollectionUtil;
import com.xiaoleilu.hutool.util.StrUtil;

import java.util.List;

/**
 * 参数错误异常
 * <p>
 * 参数错误包括：
 * 1. 函数传递的参数错误
 * 2. 前端传递的数据不合法
 * 3. ...
 * <p>
 * 这个类会被统一处理返回为标准的 {code:-1,msg:message}
 *
 * @author user
 */
public class ParamException extends RuntimeException {
    public ParamException(String message) {
        super(message);
    }

    public static void throwException(String message) {
        throw new ParamException(message);
    }

    public static void throwIf(String message, boolean enable) {
        if (enable) {
            throwException(message);
        }
    }

    public static void throwIfNull(String message, Object object) {
        if (null == object) {
            throwException(message);
        }
    }

    public static void throwIfAnyNull(String message, Object... objects) {
        for (Object object : objects) {
            if (null == object) {
                throwException(message);
            }
        }
    }

    public static void throwIfAnyNullContainList(String message, Object... objects) {
        for (Object object : objects) {
            if (object instanceof List) {
                if (CollectionUtil.isEmpty((List)object)) {
                    throwException(message);
                }
            }
            if (null == object) {
                throwException(message);
            }
        }
    }

    public static void throwIfEmpty(String message, Long object) {
        if (null == object || object.equals(0L)) {
            throwException(message);
        }
    }

    public static void throwIfAnyEmpty(String message, Long... objects) {
        for (Long object : objects) {
            if (null == object || object.equals(0L)) {
                throwException(message);
            }
        }
    }

    public static void throwIfEmpty(String message, String object) {
        if (null == object || StrUtil.isEmpty(object)) {
            throwException(message);
        }
    }

    public static void throwIfAnyEmpty(String message, String... objects) {
        for (String object : objects) {
            if (null == object || StrUtil.isEmpty(object)) {
                throwException(message);
            }
        }
    }
}
